package com.lomboardrisk.app.ee_new.xsd.wrapper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.lomboardrisk.app.ee_new.xsd.jqGrid.cfgItems.JqGridColumn;
import com.lomboardrisk.app.ee_new.xsd.jqGrid.cfgItems.JqGridConfiguration;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.AccordionItemType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.AccordionItemsType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.ActionDefinitionType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.ButtonItemType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.ButtonPanelType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.CategoriesType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.CategoryType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.CustomizablePageType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataFieldDisplayType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataFieldsDisplayType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataFilterItemType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataFilterSortEnumType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataFilterType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataFiltersType;
import com.lomboardrisk.app.ee_new.xsd.schemaSource.DataPanelType;


/**
 * <p>Title: 		EECustomizedCfgWrapper</p>
 * <p>Description: 	The wrapper for EE page Customized Configuration, which implements some additional common used methods.</p>
 * <p>Copyright: 	Copyright (c) 2007</p>
 * <p>Company: 		Lombard Risk Systems Ltd</p>
 * 
 * @author Michael Zhang
 * @created 10 Nov 2010
 * @version $Revision: 1.00 $
 */
public class EEPageCfgWrapper implements Serializable {
	
	private static final long serialVersionUID = 888173857787377350L;
	private long cfgId;
	private CustomizablePageType pageCfg;
	
	private String dataGridSortName;
	private String dataGridSortOrder;
	
	private List<CategoryType> categories;
	private List<DataFilterType> dataFilters;

	private List<DataFieldDisplayType> dataFields;
	private List<ButtonItemType> buttonItems;
	private List<ActionDefinitionType> actionBtns;
	
	private Map<String, CategoryType> categoryMap;
	private Map<String, AccordionItemType> accordionItemMap;
	private Map<String, DataFilterType> dataFilterMap;

	private Map<String, DataFieldDisplayType> dataFieldMap;
	
	private JqGridConfiguration jqGridCfg;
	private Map<String, JqGridColumn> colModelMap;
	
	public CustomizablePageType getPageCfg() {
		return pageCfg;
	}

	public void setPageCfg(CustomizablePageType pageCfg) {
		this.pageCfg = pageCfg;
		init();
	}
	
	public EEPageCfgWrapper(long cfgId, CustomizablePageType pageCfg) {
		super();
		this.cfgId = cfgId;
		this.pageCfg = pageCfg;
		init();
	}
	
	
	public long getId() {
		return cfgId;
	}

	public void setId(long id) {
		this.cfgId = id;
	}

	public String getDataGridSortName() {
		return dataGridSortName;
	}

	public void setDataGridSortName(String dataGridSortName) {
		this.dataGridSortName = dataGridSortName;
	}

	public String getDataGridSortOrder() {
		return dataGridSortOrder;
	}

	public void setDataGridSortOrder(String dataGridSortOrder) {
		this.dataGridSortOrder = dataGridSortOrder;
	}

	public List<CategoryType> getCategories() {
		return categories;
	}

	public void setCategories(List<CategoryType> categories) {
		this.categories = categories;
	}

	public List<DataFilterType> getDataFilters() {
		return dataFilters;
	}

	public void setDataFilters(List<DataFilterType> dataFilters) {
		this.dataFilters = dataFilters;
	}
	
	public List<DataFieldDisplayType> getDataFields() {
		return dataFields;
	}

	public void setDataFields(List<DataFieldDisplayType> dataFields) {
		this.dataFields = dataFields;
	}

	public List<ButtonItemType> getButtonItems() {
		return buttonItems;
	}

	public void setButtonItems(List<ButtonItemType> buttonItems) {
		this.buttonItems = buttonItems;
	}

	public List<ActionDefinitionType> getActionBtns() {
		return actionBtns;
	}

	public void setActionBtns(List<ActionDefinitionType> actionBtns) {
		this.actionBtns = actionBtns;
	}
	
	public Map<String, DataFilterType> getDataFilterMap() {
		return dataFilterMap;
	}

	public void setDataFilterMap(Map<String, DataFilterType> dataFilterMap) {
		this.dataFilterMap = dataFilterMap;
	}

	public JqGridConfiguration getJqGridCfg() {
		return jqGridCfg;
	}

	public void setJqGridCfg(JqGridConfiguration jqGridCfg) {
		this.jqGridCfg = jqGridCfg;
	}
	
	public Map<String, JqGridColumn> getColModelMap() {
		return colModelMap;
	}

	public void setColModelMap(Map<String, JqGridColumn> colModelMap) {
		this.colModelMap = colModelMap;
	}

	public CategoryType getCategoryById(String categoryId) {
		return categoryMap.get(categoryId);
	}
	
	public void addCategory(CategoryType category) {
		categories.add(category);
		categoryMap.put(category.getId(), category);
		
	}
	
	public void modifyCategory(CategoryType category) {
		CategoryType srcObj = categoryMap.get(category.getId());
		srcObj.setCollapse(category.isCollapse());
		srcObj.setDesc(category.getDesc());
		srcObj.setIcon(category.getIcon());
		srcObj.setLabel(category.getLabel());
//		try {
//			BeanUtils.copyProperties(srcObj, category);
//		} catch (IllegalAccessException e) {
//		} catch (InvocationTargetException e) {
//		}
	}

	public boolean removeCategory(String categoryId) {
		CategoryType category = categoryMap.get(categoryId);
		if(category.getItems().getItem() != null && category.getItems().getItem().size() != 0) {
			return false;
		}
		
		CategoryType srcObj = categoryMap.remove(categoryId);
		return categories.remove(srcObj);
	}
	
	public AccordionItemType getAccordionItemById(String itemId) {
		return accordionItemMap.get(itemId);
	}
	
	public void addAccordionItem(String categoryId, AccordionItemType item, DataFilterType dataFilter) {
		CategoryType category = categoryMap.get(categoryId);
		
		List<AccordionItemType> items = category.getItems().getItem();

		items.add(item);
		accordionItemMap.put(item.getId(), item);
		
		//create new DataField
		dataFilters.add(dataFilter);
		dataFilterMap.put(dataFilter.getId(), dataFilter);
		
	}
	
	public void modifyAccordionItem(AccordionItemType item) {
		AccordionItemType srcObj = accordionItemMap.get(item.getId());
		srcObj.setDesc(item.getDesc());
		srcObj.setIcon(item.getIcon());
		srcObj.setLabel(item.getLabel());
	}
	
	public void modifyAccordionItemPos(String[][] idArrs) {
		for (int i = 0; i < idArrs.length; i++) {
			String[] idArr = idArrs[i];
			List<AccordionItemType> items = new ArrayList<AccordionItemType>();
			for (int j = 0; j < idArr.length; j++) {
				String id = idArr[j];
				AccordionItemType item = accordionItemMap.get(id);
				items.add(item);
			}
			CategoryType category = categories.get(i+1);
			
			List<AccordionItemType> srcItems = category.getItems().getItem();
			srcItems.clear();
			srcItems.addAll(items);
		
		}
	}
	

	public boolean removeAccordionItem(String categoryId, String itemId) {
		CategoryType category = categoryMap.get(categoryId);
		if(category == null) {
			return false;
		}
		
		List<AccordionItemType> items = category.getItems().getItem();
		
		AccordionItemType accordionItem = null;
		for (Iterator<AccordionItemType> iter = items.iterator(); iter.hasNext();) {
			accordionItem = iter.next();
			if(accordionItem.getId().equals(itemId)) {
				removeDataFilter(accordionItem.getDataFilterRef());
				iter.remove();

				return true;
			}
		}
		return false;
		
	}
	
	public void modifyGridPro(String[] hiddenCols, Integer[] orders) {
		
		modifyEEDataGridProp4XML(hiddenCols, orders);
		modifyEEDataGridProp4Cache(hiddenCols, orders);
		
	}
	
	private void modifyEEDataGridProp4XML(String[] hiddenCols, Integer[] orders) {

		//update persistent data
		for(int i=0; i<dataFields.size(); i++ ) {
			DataFieldDisplayType dataField = dataFields.get(i);
			dataField.setHidden(false);
		}
		
		if(hiddenCols != null) {
			for(int i=0; i<hiddenCols.length; i++) {
				String colName = hiddenCols[i];
				DataFieldDisplayType column = dataFieldMap.get(colName);
				column.setHidden(true);
			}
		}
		
		if(orders != null) {
			
			List<DataFieldDisplayType> newOrder = new ArrayList<DataFieldDisplayType>();
			for(int i=0; i<orders.length; i++) {
				DataFieldDisplayType column = dataFields.get(orders[i]-1);
				newOrder.add(column);
			}
			
			dataFields.clear();
			dataFields.addAll(newOrder);
		}
	}
	
	private void modifyEEDataGridProp4Cache(String[] hiddenCols, Integer[] orders) {
		
		//update cache data
		for (Iterator<Entry<String, JqGridColumn>> iterator = getColModelMap().entrySet().iterator(); iterator.hasNext();) {
			Entry<String, JqGridColumn> entry = iterator.next();
			
			JqGridColumn column = entry.getValue();
			column.setHidden(false);
		}
		
		if(hiddenCols != null) {
			for(int i=0; i<hiddenCols.length; i++) {
				String colName = hiddenCols[i];
				JqGridColumn column = getColModelMap().get(colName);
				column.setHidden(true);
			}
		}
		
		if(orders != null) {
			String[] colNames = jqGridCfg.getColNames();
			List<JqGridColumn> colModel = jqGridCfg.getColModel();
			
			String newColNames[] = new String[orders.length];
			List<JqGridColumn> newOrders = new ArrayList<JqGridColumn>();
			for(int i=0; i<orders.length; i++) {
				newColNames[i] = colNames[orders[i]-1];
				JqGridColumn column = colModel.get(orders[i]-1);
				newOrders.add(column);
			}
			
			jqGridCfg.setColNames(newColNames);
			
			colModel.clear();
			colModel.addAll(newOrders);
		}
	}
	
	public List<DataFilterItemType> getDataFilterItemById(String dataFilerId) {
		return dataFilterMap.get(dataFilerId).getDataFilterItem();
	}
	
	public void modifyDataFilter(DataFilterType dataFilter) {
		DataFilterType srcObj = dataFilterMap.get(dataFilter.getId());
		List<DataFilterItemType> filterItems = srcObj.getDataFilterItem();
		filterItems.clear();
		filterItems.addAll(dataFilter.getDataFilterItem());
	}
	
	public boolean removeDataFilter(String dataFilterId) {
		DataFilterType srcObj = dataFilterMap.remove(dataFilterId);
		return dataFilters.remove(srcObj);
		
	}

	public DataFilterType getDataFilterById(String dataFilterId) {
		return dataFilterMap.get(dataFilterId);
	}
	
	private void init() {
		categoryMap = new HashMap<String, CategoryType>();
		accordionItemMap = new HashMap<String, AccordionItemType>();

		//init categories and its items in Accordion Section
		CategoriesType categoriesType = pageCfg.getAccordionPanel().getCategories();
		if(categoriesType == null) {
			categoriesType = new CategoriesType();
		} 
		categories = categoriesType.getCategory();
		for(int i=0; i<categories.size(); i++) {
			CategoryType category = categories.get(i);
			categoryMap.put(category.getId(), category);
			
			AccordionItemsType itemsType = category.getItems();
			
			if(itemsType == null) {
				itemsType = new AccordionItemsType();
			} 
			List<AccordionItemType> items = itemsType.getItem();
			for(int j=0; j<items.size(); j++) {
				AccordionItemType item = items.get(j);
				accordionItemMap.put(item.getId(), item);
			}
		}
		
		// init dataFilters
		dataFilterMap = new HashMap<String, DataFilterType>();
		DataFiltersType  dataFiltersType = pageCfg.getAccordionPanel().getDataFilters();
		if(dataFiltersType == null) {
			dataFiltersType = new DataFiltersType();
		}
		dataFilters = dataFiltersType.getDataFilter();
		for (Iterator<DataFilterType> iterator = dataFilters.iterator(); iterator.hasNext();) {
			DataFilterType dataFilter = iterator.next();
			dataFilterMap.put(dataFilter.getId(), dataFilter);
		}
		
		//init dataFields in Data Grid Section
		DataPanelType dataPanel = pageCfg.getDataPanel();
		
		dataGridSortName = dataPanel.getSortColumn();

		DataFilterSortEnumType sortType = dataPanel.getSortType();
		switch (sortType) {
			case NONE:
				System.out.println("OFF");
				break;
			default:
				dataGridSortOrder = sortType.value();
		}
		
		DataFieldsDisplayType datalFieldDisplayType = dataPanel.getDatalFields();
		if(datalFieldDisplayType == null) {
			datalFieldDisplayType = new DataFieldsDisplayType();
		} 
		dataFieldMap = new HashMap<String, DataFieldDisplayType>();
		dataFields = datalFieldDisplayType.getDataField();
		for(int i=0; i<dataFields.size(); i++) {
			DataFieldDisplayType dataField = dataFields.get(i);
			dataFieldMap.put(dataField.getFieldNameRef(), dataField);
			
		}
		
		//init actions in Action Section
		ButtonPanelType buttonPanel = pageCfg.getButtonPanel();
		if(buttonPanel == null) {
			buttonPanel = new ButtonPanelType(); 
		}
		buttonItems = buttonPanel.getButtonItem();
		
	}

}
